//
//  IntegralExchangeRecordVC.swift
//  甜言蜜语
//
//  Created by yingli_yanfa_liuxingyu on 16/9/20.
//  Copyright © 2016年 wl. All rights reserved.
//

import UIKit

class IntegralExchangeRecordVC: UIViewController, UITableViewDataSource, UITableViewDelegate {
    @IBOutlet weak var tableView: UITableView!
    var modelList: [integralExchangeInfo]?{
        didSet{
            self.tableView.reloadData()
        }
    }
    override func viewDidLoad() {
        super.viewDidLoad()

        self.tableView.delegate = self
        self.tableView.dataSource = self
        publicMethod.hiddleExtendCellForTableView(self.tableView)
        let cellNib = UINib(nibName: "IntegralExchangeRecordCell", bundle: nil)
        self.tableView.registerNib(cellNib, forCellReuseIdentifier: "IntegralExchangeRecordCell")
        self.getchangeRecord()
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if let count = modelList?.count {
            return count
        }
        return 0
    }
    
   func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        if(cell.respondsToSelector(Selector("setSeparatorInset:"))) {
            cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0)
        }
        if(cell.respondsToSelector(Selector("setLayoutMargins:"))) {
            cell.layoutMargins = UIEdgeInsetsMake(0, 0, 0, 0)
        }
    }
    
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        
        return 120.0
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("IntegralExchangeRecordCell", forIndexPath: indexPath) as! IntegralExchangeRecordCell
        let model = self.modelList![indexPath.row]
        cell.iv.sd_setImageWithURL(NSURL(string: model.imgsrc), placeholderImage: UIImage(named: "placeholder"))
        cell.titleLbl.text = model.name
        cell.integralNumLbl.text = model.money
        cell.changeNumLbl.text = model.integral
        cell.changeData.text = model.ptime
        cell.changeLogistics.text = model.logistics
        cell.changeProgress.text = model.state
        
        return cell
    }

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
    }
    //MARK:networking
    func getchangeRecord(){
    
        DataTool.integralExchangeRecord({ (newsArray) -> Void in
            
            guard let newDataes = newsArray else {
                return
            }
            print(newDataes)
            self.modelList = newDataes
            
        })
        
    }


}
